<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['basic']=$this->plugin_setting('setting');
//exi($return['basic']['footernav'],'error');
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>'1');
	if(isset($_RQ['group_id'])){
		$params['group_id']=intval($_RQ['group_id']);
		//$return['group_id']=$_RQ['group_id'];
	}
	//print_r($params);exit;
	//状态为显示的所有文章，排序
	$news_list=pdo_getall('jy_cmsarticle_news',$params,'','','paixu asc');
	//首页展示
	$home_list=pdo_getall('jy_cmsarticle_news',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','ishome'=>'1'),'','','paixu asc');
	//热门
	$hot_list=pdo_getall('jy_cmsarticle_news',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1','ishot'=>'1'),'','','paixu asc');
	//分类
	$fenlei=pdo_getall('jy_cmsarticle_newsgroup',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','','paixu asc');
	foreach($news_list as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] =  cfc('tools')->substr($item['desc'],0,20);
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,20);
			$item['comments']=pdo_count('jy_cmsarticle_comments',array('uniacid'=>$_SESSION['uniacid'],'cid'=>$item['id'],'status'=>'1'));
	}
	$return['post']=array();
	$return['news_list']=$news_list;
	$return['home_list']=$home_list;
	$return['hot_list']=$hot_list;
	$return['fenlei']=$fenlei;
	$return['group_id']=$_RQ['group_id']?$_RQ['group_id']:0;
	$return['account']=$_SESSION['account'];
	exi($return);
}
if($_RQ['op']=='getlist'){
	$postdata=get_postdata();
	$pindex = max(1, intval($postdata['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	if($_RQ['keyword']){
		$params['title like']='%'.trim($_RQ['keyword']).'%';
	}
    $orderby='paixu desc';
	$total='';
	$return['list']=pdo_getslice('jy_cmsarticle_news',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	$return['groups']=pdo_getall('jy_cmsarticle_newsgroup',array('uniacid'=>$_SESSION['uniacid'],'status'=>'1'),'','','paixu asc');
	foreach($return['list'] as &$item){
			$item['desc2'] = strip_tags(str_replace('&nbsp;','',$item['content']));
			$item['desc'] =  cfc('tools')->substr($item['desc'],0,20);
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['desc2'],0,20);
	}
	exi($return);
}
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
		exi('没有找到该文章，请重新选择！','error','jy_cmsarticle/news/list');
	}
	//刷新阅读次数
	pdo_update('jy_cmsarticle_news',array('readnum +='=>1),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	$item=pdo_get('jy_cmsarticle_news',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$item['msg']=pdo_getall('jy_cmsarticle_comments',array('uniacid'=>$_SESSION['uniacid'],'cid'=>$_RQ['id'],'status'=>1));
	if($item['msg']){
		$item['msgnum']=count($item['msg']);
		foreach($item['msg'] as &$row){
			$row['member']=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$row['uid']));
			$row['createtime']=date('Y-m-d h:i');
		}
	}
	$like=pdo_get('jy_cmsarticle_like',array('uniacid'=>$_SESSION['uniacid'],'user_id'=>$_SESSION['uid'],'cid'=>$_RQ['id']));
	//点赞数量=虚拟点赞数+真实点赞数量
	$lnum=pdo_count('jy_cmsarticle_like',array('uniacid'=>$_SESSION['uniacid'],'cid'=>$_RQ['id'],'clike'=>1));
	$item['likenum']=$item['likenum']+$lnum;
	$item['createtime']=date('Y-m-d',$item['createtime']);
	$return['item']=$item;
	$return['account']=$_SESSION['account'];
	$return['like']=$like;
	$return['sharedata'] = array(
		'title'=>$return['item']['title'],
		'desc'=>$return['item']['desc'],
		'imgUrl'=>tomedia($item['image']),
	);
	$return['post']=array();
	exi($return);
}
//评论
if($_RQ['op']=='setmsg'){
	$post=get_postdata();
	//print_r($post);exit;
	if(!$post['id']){
		exi('没有找到该视频，请重新选择！','error','jy_cmsarticle/news/list');
	}
	if(empty($post['msg'])){
		exi('请完善评论信息！','error'); 
	}
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'cid'=>$post['id'],
		'uid'=>$_SESSION['uid'],
		'content'=>$post['msg'],
		'status'=>1,
		'createtime'=>time(),
	
	);
	pdo_insert('jy_cmsarticle_comments',$insert);
	exi('评论成功');
}
//点赞
if($_RQ['op']=='like'){
	$post=get_postdata();
	if(!$_SESSION['uid']){
		exi('success');
	}
	$id=intval($post['id']);
	$like=intval($post['like']);
	$data=array(
		'clike'=>$post['like']?1:0,
		'updatetime'=>TIMESTAMP,
	);
	if($id){
		pdo_update('jy_cmsarticle_like',$data,array('id'=>$id,'user_id'=>$_SESSION['uid']));
	/*	if($data['clike']==1){
			pdo_update('jy_cmsarticle_news',array('likenum +='=>1),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		else{
			pdo_update('jy_cmsarticle_news',array('readnum -='=>1),array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
		}*/
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['cid']=intval($post['article_id']);
		$data['user_id']=$_SESSION['uid'];
		pdo_insert('jy_cmsarticle_like',$data);
	}
	exi('success');
}